﻿<?php
@session_start();

require_once("../../class/mysql.class.php");

  $obj = new Mysql();
  
  $obj -> set_table('user_login');
  $obj -> set_colone("(`*`)");
  $obj -> set_variables(" id_login = '" . $_SESSION["id"] . "' ");
  $user = $obj -> select_enregistrement();
  
if (isset($_REQUEST['id_sortie']))
{
	$id_article = addslashes($_REQUEST["ref_article"]);
	$id_article = addslashes($_REQUEST["id_article"]);  
	$quantite = addslashes($_REQUEST["qte"]);  
	$note = addslashes($_REQUEST["comment"]); 
	$id_user = $user['id_login'];
	$req="SELECT Quantite,designation From st_article WHERE id_article=".$id_article;
	$obj = new Mysql ();
	$resultEn = mysql_query($req);
	$rowEn = mysql_fetch_array($resultEn);
	echo $rowEn['Quantite'];
	//echo $rowEn["designation"];
	if((int)$rowEn['Quantite']<(int)$quantite){
		echo "QT < 0";
		header('location:../../list_sortie.php?action=error3&page=list_sortie&article='.$rowEn["designation"].'');
		exit();
	}

	if($rowEn['Quantite']==0){
		echo "QT = 0";
		header('location:../../list_sortie.php?action=error1&page=list_sortie&article='.$rowEn["designation"].'');
		exit();
	}
	if($rowEn['Quantite']<=0){
		echo "QT < 0";
		header('location:../../list_sortie.php?action=error2&page=list_sortie&article='.$rowEn["designation"].'');
		exit();
	}
	$reqSortie = "Select id_article, Qte from `st_attr_sortie` WHERE id_sortie='".$_REQUEST['id_sortie']."'";
	$resultQteSortie = mysql_query($reqSortie);
	$rowQteSortie = mysql_fetch_array($resultQteSortie);
	$QteSortie = $rowQteSortie['Qte'];
	$id_art = $rowQteSortie['id_article'];
	$reqQteStock= "Select Quantite from `st_article` WHERE id_article='".$id_art."'";
	$resultQteStock = mysql_query($reqQteStock);
	$rowQteStock = mysql_fetch_array($resultQteStock);
	$QteStock=$rowQteStock['Quantite'];	
	$QteFinal = $QteStock + $QteSortie - $quantite;
	
		$sql ="UPDATE `st_attr_sortie` SET  Qte='".$quantite."', note='".$note."', 
		last_modif='".$_REQUEST['dateAdd']."', last_user_modif='".$id_user."', id_unite ='".$_POST['id_unite']."'	
		WHERE id_sortie='".$_REQUEST['id_sortie']."'";
		
		$obj = new Mysql ();
		
		if(mysql_query($sql)){
			$reqInsert = "UPDATE st_article SET Quantite='".$QteFinal."' WHERE id_article='".$id_art."'";
				mysql_query($reqInsert);
		}

	
	header('location:../../list_sortie.php?action=updated&page=list_sortie');
}else{
	header('location:../../list_sortie.php?action=error&page=list_sortie');
} 